package com.i2bt.adapter;

import com.i2bt.R;
import com.i2bt.app.files.FilesFragment;
import com.i2bt.bean.FileInfoBean;
import com.i2bt.util.FileSizeConvertUtil;
import com.i2bt.util.VisibilityUtil;
import com.i2bt.view.XListView;

import android.graphics.drawable.Drawable;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 
 * Title : 我的目录列表适配器
 *
 * Description : 让列表可以切换成勾选模式, 并判断各个资源显示出对应资源类型的缩略图, 
 * 				    并与外部的碎片类做各种交互.
 *
 * CreatedTime : 2014-8-11
 *
 * @author wuliao & kakaxi & yi
 *
 * @version 1.0
 *
 */

public class FilesListAdapter extends BaseAdapter{
	
	private FilesFragment filesFragment;
	
	private List<FileInfoBean> fileInfoList;
	
	private SparseBooleanArray checkBoolArr;
	
	private boolean isChecked = false;
	
	private boolean isClickable = true;
	
	public FilesListAdapter(FilesFragment filesFragment, List<FileInfoBean> fileInfoList) {
		this.filesFragment = filesFragment;

		if (fileInfoList != null) {
			this.fileInfoList = fileInfoList;
		}
		else {
			this.fileInfoList = new ArrayList<FileInfoBean>();
		}
		
		checkBoolArr = new SparseBooleanArray();
	}
	
	@Override
	public int getCount() {
		return fileInfoList.size();
	}

	@Override
	public Object getItem(int position) {
		return fileInfoList.get(position);
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {
			
			//解析xml视图
			convertView = LayoutInflater.from(filesFragment.getActivity()).inflate(R.layout.item_files_list, null);
			
			//新建一个内部视图持有者类, 放入对应的视图
			holder = new ViewHolder();
			holder.file_view = (ImageView) convertView.findViewById(R.id.file_img);
			holder.file_name = (TextView) convertView.findViewById(R.id.file_name);
			holder.file_length = (TextView) convertView.findViewById(R.id.file_length);
			holder.select_cbox = (CheckBox) convertView.findViewById(R.id.file_select_checkbox);
			
			//把视图持有者存储进总视图里, 方便以后使用
			convertView.setTag(holder);
		}
		else {
			//获取总视图里面储存的持有者
			holder = (ViewHolder) convertView.getTag();
		}
		//获取资源信息数组里对应位置的bean
		FileInfoBean fileInfoBean = fileInfoList.get(position);
		//判断此资源该使用的图标
		String fileName = fileInfoBean.getFileName();
		Drawable drawable = getFileIcon(fileName, fileInfoBean.isFile());
		
		//将bean中对应的值显示在视图上
		holder.file_view.setImageDrawable(drawable);
		holder.file_name.setText(fileName);
		if (fileInfoBean.isFile()) {
			holder.file_length.setText(FileSizeConvertUtil.autoConvert(
					fileInfoBean.getFileLength()));
		}
		VisibilityUtil.setVisibilityUI(holder.file_length, fileInfoBean.isFile());
		
		holder.select_cbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				//修改资源勾选列表对应的项的勾选标志
				checkBoolArr.put(position, isChecked);
				 
				//当勾选第一个按钮的时候和刚变成选择状态的时候
				if (getCheckedCount() == 1 && !getChecked()) {
					//标志当前为选择状态
					setChecked(true);
					//弹出底部的tabbar
					filesFragment.changeType(true);
					filesFragment.checkNumberCheck();
					
					//当选中的是最下面的项时, 那个项会向上滚动
					XListView listView = filesFragment.getListView();
					if (position == listView.getLastVisiblePosition()) {
						listView.smoothScrollToPosition(position);
					}
				}
				notifyDataSetChanged();

			}
		});
		
		//使用勾选按钮布尔数组来设置勾选按钮的状态
		Boolean isChecked = checkBoolArr.get(position);
		if (isChecked != null) {
			holder.select_cbox.setChecked(isChecked);
		}
		//实际的设置点击状态
		holder.select_cbox.setClickable(isClickable);
		
		// 不显示第一层和第二层的选择按钮, 不能选择homebox和硬盘
//		int currentPathAt = filesFragment.getCurrentPathAt();
//		if (currentPathAt > 0) {
//			VisibilityUtil.setVisibilityUI(holder.select_cbox, true);
//		}
//		else {
//			VisibilityUtil.setVisibilityUI(holder.select_cbox, false);
//		}
		
		return convertView;
	}
	
	/**
	 * 
	 * Title : 内部持有者类
	 *
	 * Description : 包括了改视图需要的子视图
	 *
	 * CreatedTime : 2014-8-13
	 *
	 * @author wuliao & kakaxi & yi
	 *
	 * @version 1.0
	 *
	 */
	private class ViewHolder {
		
		ImageView file_view;
		
		TextView file_name;
		
		TextView file_length;
		
		CheckBox select_cbox;
	}
	 
	/**
	 * 
	 * @param fileName
	 * @param isFile
	 * @return
	 */
	private Drawable getFileIcon(String fileName, boolean isFile) {
		int imgId = 0;
		
		if (filesFragment.getCurrentPathAt() == 1) {
			imgId = R.drawable.files_icon_homebox;
			return filesFragment.getResources().getDrawable(imgId);
		}
		
		if (isFile) {
			
			String suffix = "";
			if (fileName.contains(".")) {
				suffix = fileName.substring(fileName.lastIndexOf("."))
						.toLowerCase(Locale.CHINA);
			}	
			
			if (suffix.equals(".mp3") || suffix.equals(".acc") 
					|| suffix.equals("APE") || suffix.equals("wav")) {
				imgId = R.drawable.files_icon_music;
			}
			else if (suffix.equals(".png") || suffix.equals(".jpg")
					|| suffix.equals(".bmp")) {
				imgId = R.drawable.files_icon_image;
			}
			else if (suffix.equals(".avi") || suffix.equals(".rmvb")
					|| suffix.equals(".mpg") || suffix.equals(".3gp")
					|| suffix.equals(".mp4") || suffix.equals(".rm")
					|| suffix.equals(".mkv") || suffix.equals(".flv")) {
				imgId = R.drawable.files_icon_movie;
			}
			else if (suffix.equals(".txt")){
				imgId = R.drawable.files_icon_txt;
			}
			else {
				imgId = R.drawable.files_icon_other;
			}
			
		}
		else {
			if (fileName.contains(":")) {
				imgId = R.drawable.files_icon_disk;
			}
			else {
				imgId = R.drawable.files_icon_folder;
			}
		}
		
		
		return filesFragment.getResources().getDrawable(imgId);
	}
	
	public void setAllChecked(boolean isChecked) {
		for (int i = 0; i < fileInfoList.size(); i++) {
			checkBoolArr.put(i, isChecked);
		}
		notifyDataSetChanged();
	}
	
	public void setChecked(boolean isChecked) {
		this.isChecked = isChecked;
	}
	
	public boolean getChecked() {
		return isChecked;
	}

	public void setCheckedItem(int position, boolean isChecked) {
		checkBoolArr.put(position, isChecked);
	}
	
	public boolean getCheckedItem(int position) {
		return checkBoolArr.get(position);
	}
	
	public int getCheckedCount() {
		int n = 0;
		for (int i = 0; i < getCount(); i++) {
			if (getCheckedItem(i)) {
				n++;
			}
		}
		return n;
	}
	
	public List<Integer> getCheckedList() {
		List<Integer> filesCheckedList = new ArrayList<Integer>();
		for (int i = 0; i < getCount(); i++) {
			if (getCheckedItem(i)) {
				filesCheckedList.add(i);
			}
		}
		return filesCheckedList;
	}
	
	public void setData(List<FileInfoBean> fileInfoList) {
		this.fileInfoList = fileInfoList;
		notifyDataSetInvalidated();
	}
	
	public void setClickable(boolean isClickable) {
		this.isClickable = isClickable;
	}

}
